//
//  LCZPicDetailViewController.m
//  PersonalNews
//
//  Created by dllo on 15/12/1.
//  Copyright © 2015年 changshuhua. All rights reserved.
//

#import "LCZPicDetailViewController.h"
#import "LCZPicDetailcell.h"

@interface LCZPicDetailViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, strong) LCZBaseCollectionView *collectionView;
@property(nonatomic, strong) LCZBaseLabel *titleLabel;
@property(nonatomic, strong) LCZBaseLabel *pageLabel;
@property(nonatomic, strong) LCZBaseScrollView *scrollView;
@property(nonatomic, strong) NSMutableArray *picArr;
@property(nonatomic, strong) LCZBaseLabel *detailLabel;

@end

@implementation LCZPicDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    
    [self createCollectionView];
    [self createView];
    [self createDate];
}

- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(SCREENWIDTH, SCREENHEIGHT);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[LCZBaseCollectionView alloc] initWithFrame:CGRectMake(0, 100, SCREENWIDTH, 350) collectionViewLayout:layout];
    [self.view addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.pagingEnabled = YES;
    [self.collectionView registerClass:[LCZPicDetailcell class] forCellWithReuseIdentifier:@"collection"];
    
    
}
- (void)createView {
    
    self.scrollView = [[LCZBaseScrollView alloc] initWithFrame:CGRectMake(0, SCREENHEIGHT - 164, SCREENWIDTH - 10, 100)];
    [self.view addSubview:self.scrollView];

    
    self.detailLabel = [[LCZBaseLabel alloc] initWithFrame:CGRectMake(5, 0, SCREENWIDTH - 10, 10)];
    [self.scrollView addSubview:self.detailLabel];
//    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.width.height.equalTo(self.scrollView);
//        make.left.equalTo(self.view).with.offset(5);
//        make.bottom.equalTo(self.view).with.offset(0);
//        
//    }];
    self.detailLabel.numberOfLines = 0;
//    self.detailLabel.backgroundColor = [UIColor yellowColor];
    
    self.titleLabel = [[LCZBaseLabel alloc] init];
    [self.view addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.view).with.offset(10);
        make.bottom.equalTo(self.detailLabel.mas_top).with.offset(0);
        make.width.mas_equalTo(SCREENWIDTH - 80);
        make.height.mas_equalTo(30);
        
    }];
    self.titleLabel.numberOfLines = 0;
    
    self.pageLabel = [[LCZBaseLabel alloc] initWithFrame:CGRectMake(SCREENWIDTH - 80, SCREENHEIGHT - 194, 75, 30)];
    [self.view addSubview:self.pageLabel];
    self.pageLabel.textColor = [UIColor whiteColor];
    self.pageLabel.textAlignment = NSTextAlignmentRight;

    
    self.titleLabel.textColor = [UIColor whiteColor];
    self.detailLabel.textColor = [UIColor whiteColor];
    
    
}

- (void)createDate {
    [ZLPNetWorkTool getWithURL:self.URLStr success:^(id responseObject) {
        
        NSDictionary *dic = responseObject;
        self.picArr = [NSMutableArray array];
        self.picArr = dic[@"photos"];
        self.titleStr = dic[@"setname"];
        self.titleLabel.text = self.titleStr;
        self.pageLabel.text = [NSString stringWithFormat:@"1/%ld", self.picArr.count];
        
        [self.collectionView reloadData];
        
    } failure:^(NSError *error) {
       
        NSLog(@"%@", error);
        
    }];
}


#pragma mark -- collection协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSLog(@"%ld", self.picArr.count);
    return self.picArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LCZPicDetailcell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collection" forIndexPath:indexPath];
    
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", self.picArr[indexPath.row][@"imgurl"]]] placeholderImage:[UIImage imageNamed:@"placeholder"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        cell.imageView.frame = CGRectMake(0, (cell.frame.size.height - image.size.height / image.size.width * SCREENWIDTH) / 2 - 64 / 667.0 * SCREENWIDTH, SCREENWIDTH, image.size.height / image.size.width * SCREENWIDTH);
    }];
    
    
    self.detailLabel.text = self.picArr[indexPath.row][@"note"];
    [self.detailLabel sizeToFit];
    self.scrollView.contentSize = CGSizeMake(0, self.detailLabel.frame.size.height);
    self.pageLabel.text = [NSString stringWithFormat:@"%ld/%ld", indexPath.row + 1, self.picArr.count];
    
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
